/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_bed_devicegroup;
import org.testis.orm.classes.T_bed_devicegroupCriteria;
import org.testis.orm.classes.T_bed_devicegroupFactory;

/**
 * 
 * @author selcuk
 */
public class DeviceGroupDataHandler extends AtofOrmFlyweightFactory {

    private static DeviceGroupDataHandler m_reference;

    private DeviceGroupDataHandler() {
	loadDeviceGroups();
    }

    public static DeviceGroupDataHandler newInstance() {
	if (m_reference == null) {
	    m_reference = new DeviceGroupDataHandler();
	}
	return m_reference;
    }

    public Collection getDeviceGroups() {
	return FLY_WEIGHT_DATA.values();
    }

    public T_bed_devicegroup[] getDeviceGroupArray() {
	Collection coll = getDeviceGroups();
	T_bed_devicegroup[] result = new T_bed_devicegroup[coll.size()];
	coll.toArray(result);
	return result;
    }

    public LinkedList<T_bed_devicegroup> getDeviceGroupList() {
	Collection col = getDeviceGroups();
	return new LinkedList<T_bed_devicegroup>(col);
    }

    public HashMap getDeviceGroupMap() {
	return new HashMap(FLY_WEIGHT_DATA);
    }

    public int getNumberOfDeviceGroups() {
	return FLY_WEIGHT_DATA.size();
    }

    public T_bed_devicegroup getDeviceGroupById(int id) {
	T_bed_devicegroup result = null;
	Object obj = getFlyweight(Integer.valueOf(id));
	if (obj != null) {
	    result = (T_bed_devicegroup) obj;
	}
	return result;
    }

    public int addDeviceGroup(String name) {
	T_bed_devicegroup dg = createDeviceGroup(name);
	int id = 0;
	if (dg != null) {
	    id = dg.getId();
	    putFlyweight(Integer.valueOf(id), dg);
	}
	return id;
    }

    public boolean saveDeviceDefinition(int id, String name) {
	return updateDeviceGroup(id, name);
    }

    @Override
    protected Object loadFlyweight(Object key) {
	T_bed_devicegroup obj = readDeviceGroupById((Integer) key);
	return obj;
    }

    private void loadDeviceGroups() {
	T_bed_devicegroup[] groups = readDeviceGroups();
	if (groups != null) {
	    for (int i = 0; i < groups.length; i++) {
		putFlyweight(Integer.valueOf(groups[i].getId()), groups[i]);
	    }
	}
    }

    private T_bed_devicegroup createDeviceGroup(String name) {
	T_bed_devicegroup result = null;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_devicegroup obj = new T_bed_devicegroup();
		obj.setName(name);
		obj.save();
		t.commit();
		result = obj;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private boolean updateDeviceGroup(int id, String name) {
	boolean result = false;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_devicegroup obj = getDeviceGroupById(id);
		if (obj != null) {
		    obj.setName(name);
		    result = obj.save();
		}
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_devicegroup readDeviceGroupById(int id) {
	T_bed_devicegroup result = null;
	try {
	    T_bed_devicegroupCriteria criteria = new T_bed_devicegroupCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_devicegroup();
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_devicegroup[] readDeviceGroups() {
	T_bed_devicegroup[] result = null;
	try {
	    result = T_bed_devicegroupFactory.listT_bed_devicegroupByQuery(
		    null, null);
	    if (result == null) {
		result = new T_bed_devicegroup[0];
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }
}
